package com.bee.beemanage.api.controller;

import com.bee.beemanage.api.entity.SysMenu;
import com.bee.beemanage.api.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bee.beemanage.system.common.utils.result.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import static com.bee.beemanage.system.common.utils.result.ResultTool.success;

@RestController
@RequestMapping("/bee-manage/sys-menu")
@Api(value ="菜单表",tags = "菜单表")
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    /**
    * 新增菜单表
    * @param sysMenu
    * @return
    */
    @ApiOperation(value = "新增菜单表", notes = "新增菜单表")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:menu:save')")
    public Result save(@RequestBody SysMenu sysMenu) {
        sysMenu.setCreateTime(LocalDateTime.now());
        return success(sysMenuService.save(sysMenu));
    }
    /**
    * 修改菜单表
    * @param sysMenu
    * @return
    */
    @ApiOperation(value = "修改菜单表", notes = "修改菜单表")
    @PostMapping("/edit")
    @PreAuthorize("hasAnyAuthority('sys:menu:edit')")
    public Result edit(@RequestBody SysMenu sysMenu) {
        sysMenu.setUpdateTime(LocalDateTime.now());
        return success(sysMenuService.updateById(sysMenu));
    }
    /**
    * 根据id删除菜单表
    * @param id
    * @return
    */
    @ApiOperation(value = "通过id删除菜单表", notes = "通过id删除菜单表")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys:menu:removeById')")
    public Result removeById(@PathVariable String id) {
        return sysMenuService.deleteMenuById(id);
    }
    /**
    * 根据id批量删除菜单表
    * @param ids
    * @return
    */
    @ApiOperation(value = "通过id批量删除菜单表", notes = "通过id批量删除菜单表")
    @PostMapping("/batch")
    @PreAuthorize("hasAnyAuthority('sys:menu:removeByIds')")
    public Result removeByIds(@RequestBody String[] ids) {
    List<String> idsList = Arrays.asList(ids);
        return success(sysMenuService.removeByIds(idsList));
    }
    /**
    * 菜单表分页查询
    * @param page
    * @param sysMenu
    * @return
    */
    @ApiOperation(value = "菜单表分页查询", notes = "菜单表分页查询")
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('sys:menu:page')")
    public Result page(Page<SysMenu> page, SysMenu sysMenu) {
        return success(sysMenuService.page(page, Wrappers.query(sysMenu)));
    }

    /**
    * 根据id查询菜单表
    * @param id
    * @return
    */
    @ApiOperation(value = "通过id查询菜单表", notes = "通过id查询菜单表")
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('sys:menu:getById')")
    public Result getById(@PathVariable("id") String id) {
        return success(sysMenuService.getById(id));
    }

    /**
     * 获取菜单树
     * @return
     */
    @ApiOperation(value = "获取菜单树", notes = "获取菜单树")
    @GetMapping("/getMenuTree")
    @PreAuthorize("hasAnyAuthority('sys:menu:getMenuTree')")
    public Result getMenuTree() {
        return success(sysMenuService.selectMenuTreeByUserId(null,"1"));
    }
}